package com.yuezhu.constomview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yuezhu.R;
import com.yuezhu.constante.BusAction;
import com.yuezhu.global.ApplicationContext;

import org.greenrobot.eventbus.EventBus;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by zhao on 2016/5/18.
 */
public class DragView extends LinearLayout implements View.OnTouchListener {
    @InjectView(R.id.line)
    View line;
    @InjectView(R.id.line_layout)
    View line_layout;
    @InjectView(R.id.color_view)
    View colorView;
    @InjectView(R.id.img1)
    ImageView img1;
    @InjectView(R.id.img2)
    ImageView img2;
    GetLocation getLocation;
    @InjectView(R.id.img1_text)
    TextView img1Text;
    RelativeLayout.LayoutParams text1Params, text2Params;
    @InjectView(R.id.img2_text)
    TextView img2Text;
    @InjectView(R.id.top_text)
    TextView topText;
    @InjectView(R.id.bottom_text)
    TextView bottomText;
    @InjectView(R.id.fenqu_text)
    TextView fenquText;
    @InjectView(R.id.other_info)
    TextView otherInfo;
    private int max = 300;
    private RelativeLayout.LayoutParams img1Params;
    private RelativeLayout.LayoutParams img2Params;
    private boolean isPartInfo = false;

    public void setMax(int m) {
        max = m;
    }

    public void setFenquText(String s) {
        if (fenquText != null) {
            fenquText.setText(s);
        }
    }

    public TextView getFenquText() {
        return fenquText;
    }

    public TextView getOtherInfo() {
        return otherInfo;
    }

    public DragView(Context context, boolean isPartInfo) {
        super(context);
        this.isPartInfo = isPartInfo;
        initview(context);
    }

    public DragView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initview(context);
    }

    public DragView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initview(context);
    }

    int iconHeight = getResources().getDimensionPixelSize(R.dimen.drag_view_icon_height);
    int screenHeight = ApplicationContext.getInstance().getScreen_height() / 3;
    int calHeight = screenHeight - iconHeight;
    int screenWidth = ApplicationContext.getInstance().getScreen_width();
    int lastY;

    public void setListener(GetLocation getLocation) {
        this.getLocation = getLocation;
    }

    private void initview(Context context) {
//        BusProvider.getInstance().register(this);
        View view = LayoutInflater.from(context).inflate(R.layout.view_drig, this);
        if (isPartInfo) {
            view.setBackgroundResource(R.drawable.part_info_back);
        } else {
            view.setBackgroundResource(R.drawable.dian_ti_num_back);
        }
        ButterKnife.inject(this, view);
        text1Params = (RelativeLayout.LayoutParams) img1Text.getLayoutParams();
        text2Params = (RelativeLayout.LayoutParams) img2Text.getLayoutParams();
        img1.setOnTouchListener(this);
        img2.setOnTouchListener(this);
        img1Params = (RelativeLayout.LayoutParams) img1.getLayoutParams();
        img2Params = (RelativeLayout.LayoutParams) img2.getLayoutParams();
        if (isPartInfo) {
            colorView.setVisibility(GONE);
            topText.setVisibility(GONE);
            bottomText.setVisibility(GONE);
            img1.setVisibility(VISIBLE);
            img2.setVisibility(VISIBLE);
            img1Text.setVisibility(VISIBLE);
            img2Text.setVisibility(VISIBLE);
            line.setVisibility(VISIBLE);
        } else {
            colorView.setVisibility(VISIBLE);
            topText.setVisibility(VISIBLE);
            bottomText.setVisibility(VISIBLE);
            img1.setVisibility(GONE);
            img2.setVisibility(GONE);
            img1Text.setVisibility(GONE);
            img2Text.setVisibility(GONE);
            line.setVisibility(GONE);
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
//        BusProvider.getInstance().unregister(this);
    }

    public void setSkyLobby(boolean is) {
        img2.setImageResource(is ? R.drawable.yuan : R.drawable.yundan_up);
    }

    int index;

    public void setPosition(int index) {
        this.index = index;
    }

    public void setLocationMinAndMax(int max, int min) {
        if (min < 2) {
            min = 2;
        }
        if (max < 2) {
            max = 2;
        }
        if (max <= min) {
            max = min + 1;
        }
        if (isPartInfo) {
            screenHeight = line_layout.getHeight();
            calHeight = line_layout.getHeight() - iconHeight;
            int top = calHeight - max * calHeight / this.max;
            int bottom = calHeight - min * calHeight / this.max;
            img1Text.setText(max + "F");
            img2Text.setText(min + "F");
            RelativeLayout.LayoutParams params1 = (RelativeLayout.LayoutParams) img1.getLayoutParams();
            params1.topMargin = top;
            img1.setLayoutParams(params1);
            RelativeLayout.LayoutParams params11 = (RelativeLayout.LayoutParams) img1Text.getLayoutParams();
            params11.topMargin = top;
            img1Text.setLayoutParams(params11);
            RelativeLayout.LayoutParams params2 = (RelativeLayout.LayoutParams) img2.getLayoutParams();
            params2.topMargin = bottom;
            img2.setLayoutParams(params2);
            RelativeLayout.LayoutParams params22 = (RelativeLayout.LayoutParams) img2Text.getLayoutParams();
            params22.topMargin = bottom;
            img2Text.setLayoutParams(params22);
        } else {
            screenHeight = line_layout.getHeight();
            calHeight = line_layout.getHeight();
            int top = calHeight - max * calHeight / this.max;
            int bottom = calHeight - min * calHeight / this.max;
            topText.setText(max + "");
            bottomText.setText(min + "");
            RelativeLayout.LayoutParams params1 = (RelativeLayout.LayoutParams) colorView.getLayoutParams();
            params1.topMargin = top;
            params1.height = bottom - top;
            colorView.setLayoutParams(params1);
        }
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
//        EventBus.getDefault().post(new BusAction.PartInfoDragViewClicked(index));
        if (!isPartInfo) {
            return true;
        }
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                lastY = (int) event.getRawY();
                break;

            case MotionEvent.ACTION_MOVE:
                int dy = (int) event.getRawY() - lastY;
                int top = v.getTop() + dy;
                if (top <= 0) {
                    top = 0;
                }
                if (top >= screenHeight - v.getHeight()) {
                    top = screenHeight - v.getHeight();
                }
                int floor = -1;
                switch (v.getId()) {
                    case R.id.img1:
                        floor = max - top * max / calHeight;
                        if (floor < 2 || floor <= Integer.parseInt(img2Text.getText().toString().replace("F", ""))) {
                            return true;
                        }
                        text1Params.topMargin = top;
                        img1Params.topMargin = text1Params.topMargin;
                        img1.setLayoutParams(img1Params);
                        img1Text.setLayoutParams(text1Params);
                        img1Text.setText(floor + "F");
                        v.layout(v.getLeft(), top, v.getLeft() + v.getWidth(), top + v.getHeight());
                        break;
                    case R.id.img2:
                        floor = max - top * max / calHeight;
                        if (floor < 2 || floor >= Integer.parseInt(img1Text.getText().toString().replace("F", ""))) {
                            return true;
                        }
                        text2Params.topMargin = top;
                        img2Params.topMargin = text2Params.topMargin;
                        img2.setLayoutParams(img2Params);
                        img2Text.setLayoutParams(text2Params);
                        img2Text.setText(floor + "F");
                        v.layout(v.getLeft(), top, v.getLeft() + v.getWidth(), top + v.getHeight());
                        break;
                }
                EventBus.getDefault().post(new BusAction.PartInfoDragViewClicked(index, Integer.parseInt(img2Text.getText().toString().replace("F", "")), Integer.parseInt(img1Text.getText().toString().replace("F", ""))));
                lastY = (int) event.getRawY();
                break;
            case MotionEvent.ACTION_UP:
                break;
        }
        return true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (isPartInfo) {
            EventBus.getDefault().post(new BusAction.PartInfoDragViewClicked(index, Integer.parseInt(img2Text.getText().toString().replace("F", "")), Integer.parseInt(img1Text.getText().toString().replace("F", ""))));
        } else {
            EventBus.getDefault().post(new BusAction.DiantiDragViewClicked(index));
        }
//        BusProvider.getInstance().post(
//                new DataChangedEvent(BusAction.MYMESS_CLICK, index));
        return super.onTouchEvent(event);
    }

    private void set() {
        int top1 = Integer.parseInt(img1Text.getText().toString().replace("F", "")) * screenHeight / max;
        RelativeLayout.LayoutParams params1 = (RelativeLayout.LayoutParams) img1.getLayoutParams();
        params1.topMargin = top1;
        img1.setLayoutParams(params1);
        int top2 = Integer.parseInt(img2Text.getText().toString().replace("F", "")) * screenHeight / max;
        RelativeLayout.LayoutParams params2 = (RelativeLayout.LayoutParams) img2.getLayoutParams();
        params2.topMargin = top2;
        img2.setLayoutParams(params2);
    }

    public interface GetLocation {
        void callback(int top);
    }

    public void setColorView(int color) {
        colorView.setBackgroundColor(color);
    }
//    @Subscribe
//    // 订阅事件DataChangedEvent
//    public void setCardEvent(DataChangedEvent event) {
//
//        switch (event.getAction()) {
//            case BusAction.MYMESS_CLICK:
//                String code = event.getCode();
//                LogUtils.e("nn", index + "   index  code  " + code);
//                if (!(index + "").equals(code)) {
//                    setSelected(false);
//                    img2.setSelected(false);
//                }
//                break;
//
//        }
//    }

}
